/**
 * 内容管理相关逻辑
 * @type {*}
 */
var pageObj = $('#page');
var pageSize = 50;
var cPage = 1;
var dataList;

/**
 * 初始化分页
 */
function init() {
    loadData(cPage);
}

/**
 * 查询列表
 */
function searchList() {
    loadData(cPage);
}

/**
 * 处理分页
 * @param data
 */
function processPage(data) {
    pageObj.children().remove();
    pageObj.append('<div id="create_page_num"></div>');
    pageObj.find("#create_page_num").createPage({
        pageCount: data.pages,
        current: data.pageNum,
        backFn: function (p) {
            loadData(p, pageSize);
        }
    });
}

/**
 * 加载列表
 * @param pageNo
 */
function loadData(pageNo) {
    $.ajax({
        url: "/manage/content/seedlist",
        type: 'POST',
        data: {
               pageNum: pageNo,
               pageSize: pageSize,
               remark: $('#remark').val(),
               title: $('#title').val(),
               type: $('#type').val(),
               createTimeStart:$('#createTimeStart').val(),
               createTimeEnd:$('#createTimeEnd').val()
        },
        success: function (data) {
            dataList = data.data.list;
            var allDataHtml = "";
            for (var i = 0; i < dataList.length; i++) {
                var cData = dataList[i];
                var cHtml =
                    "<tr data-id=\"" + cData.id + "\" data-no=\"" + cData.no + "\">\n" +
                    "<td>" + cData.id + "</td>\n" +
                    "<td>" + cData.title + "</td>\n" +
                    "<td>" + cData.remark + "</td>\n";
                    if(cData.type == 'AUTO'){
                       cHtml += "<td>自动爬取</td>\n";
                    }else{
                       cHtml += "<td>手动导入</td>\n";
                    }
                    cHtml += "<td>" + cData.url + "</td>\n" +
                    "<td>" + cData.createTime + "</td>\n" +
                    "<td>" + cData.firstDiscovery + "</td>\n" +
                    "<td>" + cData.recentlyDiscovery + "</td>\n" +
                    "<td>";
                    if (cData.useCount > 0) {
                        cHtml += "已编辑";
                    } else {
                        cHtml += "未编辑";
                    }
                    cHtml += "</td><td>\n" +
                    "<a href=\"javascript:editContent('" + cData.no + "');\">编辑</a>\n";
                    if(cData.useCount > 0){
                        cHtml += "<a href=\"javascript:publishCms('" + cData.no + "');\" class=\"red-link\">发布</a>\n";
                    }
                    cHtml += "</td>\n" +
                    "</tr>";
                allDataHtml += cHtml;
            }
            $("#resultList").html();
            $("#resultList").html(allDataHtml);

            // 处理分页
            cPage = pageNo;
            processPage(data.data);
        }
    });
}

/**
 * 新建种子链接
 */
function createSeed() {
    var arr = '<div class="createTarget">' +
        '<ul>' +
        '<li>' +
        '<span>标题：</span>' +
        '<input type="text" id="ctitle" name="ctitle" value="" />' +
        '</li>' +
        '<li>' +
        '<span>url：</span>' +
        '<textarea rows="3" cols="20" name="curl" id="curl"></textarea>' +
        '</li>' +
        '</ul>' +
        '</div>';

    var config = {
        title: "手动导入种子",
        body: arr,
        btn: [
            {
                title: '确定',
                className: 'button btn-ok btn-radius',
                dimiss: false,
                callback: function(){
                    return checkQualified();
                }
            },
            {
                title: '取消',
                className: 'button btn-blue-line btn-radius',
                dimiss: false,
                callback: function(){
                    return true;
                }
            }
        ]
    };
    createModalDom(config);
    $('#myModal').modal('show');
}

//检查输入的内容
function checkQualified() {
    var _title = $("#ctitle"),
        _url = $("#curl");

    $.ajax({
        url: "/manage/content/addSeed",
        type: "post",
        data: {
            title: _title.val(),
            url: _url.val()
        },
        success: function(data){
            if (data.status) {
                alert("导入种子成功");
                loadData(cPage);
            } else {
                alert("导入种子失败");
            }
        },
        fail: function(data) {
            console.error("服务器内部错误");
        }
    });
    return true;
}

/**
 * 发布到Cms
 * @param recordNo
 * @returns {boolean}
 */
function publishCms(recordNo) {

    if ("" != recordNo && undefined != recordNo) {
        $.ajax({
            url: "/manage/content/publish",
            type: "post",
            data: {
                recordNo: recordNo
            },
            success: function(data){
                if (data.status) {
                    alert("发布成功");
                    loadData(cPage);
                    return true;
                } else {
                    alert(data.message);
                    return false;
                }
            },
            fail: function(data) {
                console.error("服务器内部错误");
            }
        });
        return true;
    } else {
        return true;
    }
}

/**
 * 编辑正文操作
 * @param recordNo
 */
function editContent(recordNo) {

    window.open("/manage/readedit/index.html?recordNo="+recordNo);
}



